package com.xs.langchain4j_springboot.controller;

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.store.embedding.EmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/rag")
public class RagController {

    @Autowired
    @Qualifier("ollamaStreamingChatModel")
    private StreamingChatLanguageModel qwen3Stream;

    @Autowired
    @Qualifier("ollamaChatModel")
    private ChatLanguageModel qwen3;

    @Autowired
    private EmbeddingModel embeddingModel;

    @Autowired
    private EmbeddingStore embeddingStore;

    //rag接口
    @CrossOrigin(origins = "*")
    @GetMapping(value = "/txt")     //todo  实现文档（txt、md等）上传
    public String rag(@RequestParam TextSegment message) {
        if (!StringUtils.hasText(message.text())) {
            return "请输入内容";
        }

        try {
            /*QwenEmbeddingModel embeddingModel = QwenEmbeddingModel.builder()
                    .apiKey("sk-8675e9b7aae54b70ba0399a37d8ca0a1")
                    .build();*/

            Response<Embedding> response = embeddingModel.embed(message);

            if (response != null) {
                // 将向量存储到Redis中
                embeddingStore.add(response.content(),  message);


                Embedding embedding = response.content();
                System.out.println("----------------向量的维度------------------："  + embedding.dimension());
                //只展示前10个维度
                float[] vector = embedding.vector();
                StringBuilder sb = new StringBuilder("向量化成功！前10维为: [");
                for (int i = 0; i < Math.min(10, vector.length); i++) {
                    sb.append(String.format("%.4f", vector[i]));
                    if (i < 9 && i < vector.length - 1) sb.append(", ");
                }
                sb.append("]");
                return sb.toString();
            } else {
                return "向量化失败";
            }

        } catch (Exception e) {
            e.printStackTrace();
            return "发生错误：" + e.getMessage();
        }
    }


    @CrossOrigin(origins = "*")
    @PostMapping(value = "/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String ragFile(@RequestParam MultipartFile file) {
        return "上传成功";
    }

    //检测模型维度
    @GetMapping("/test-dim")
    public int testDim() {
        Response<Embedding> response = embeddingModel.embed(TextSegment.from("test"));
        System.out.println(response.content().vector().length);
        return response.content().vector().length;
    }
}
